﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace HotelRegistrationSystem.Models.AttributeValidators
{
    public class MinDateAttribute : ValidationAttribute 
    {
        private const string DateFormat = "dd MMMM yy";
        private const string DefaultErrorMessage = "'{0}' must be a date after {1:d}.";

        public MinDateAttribute() : base(DefaultErrorMessage)
        {
        }

        public override bool IsValid(object value)
        {
            if (value == null || !(value is DateTime))
            {
                return true;
            }
            DateTime dateValue = (DateTime)value;
            return DateTime.Now <= dateValue;
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, 
                DateTime.Now.ToString("dd MMMM yyyy"));
        }

        private static DateTime ParseDate(string dateValue)
        {
            return DateTime.ParseExact(dateValue, DateFormat, CultureInfo.InvariantCulture);
        }
    }
}